//获取应用实例
var app = getApp();
var config = require('../../config');
const $ = require('../../utils/utils');
const version = config.appversion;
const user = require('../../manager/userManager')




Page({

    /**
     * 页面的初始数据
     */
    data: {
        userPhone: "",
        isTime: 1,
        rest_count: 60,
        version_data: version
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

        //如果当前微信已经是老用户即缓存过用户信息直接到首页
        if (wx.getStorageSync('userid') || wx.getStorageSync('usertoken')) {
            let src = decodeURIComponent(options.q)
            let index = src.indexOf("scanCode?")
            if (src.indexOf("scanCode?") > 0) {
                let scanCode = src.substr(index + 1, src.length + 1)
                console.warn(scanCode)
            }

            wx.reLaunch({
                url: config.page.index,
            });
            return;
        }


    },

    bindKeyInput: function (e) {
        console.log(e);
        this.setData({
            userPhone: e.detail.value
        });
    },


    time_down: function () {
        var that = this;
        var second = 60;
        var timer = null;
        timer = setInterval(function () {
            second -= 1;
            if (second > 0) {
                that.setData({
                    rest_count: second
                });
            } else {
                clearInterval(timer);
                that.setData({
                    isTime: 1
                });
            }
        }, 1000);
    },

    //获取验证码
    getYzcode: function () {
        var that = this;
        if (that.data.userPhone.length != 11) {
            wx.showToast({

                title: '请输入11位手机号!',

                icon: 'loading',

                duration: 1500

            });

            setTimeout(function () {

                wx.hideToast()

            }, 2000);
        } else {
            that.setData({
                isTime: 0
            });
            that.time_down();
            let data = {
                mobile_no: that.data.userPhone
            }
            let url = $.getUrl(config.getRegisterCode, data)
            $.get(url).then(data => {
                wx.showModal({
                    content: data.message,
                    showCancel: false,
                    confirmColor: "#FF3B30",
                    success: function (res) {
                        if (res.confirm) {
                            console.log('用户点击确定')
                        }
                    }
                });
            })
        }
    },

    formSubmit: function (e) {
        console.log(e);
        if (!e.detail.value.user_code) {
            $.showToast("验证码不得为空")
        } else {
            wx.showLoading({

                title: "正在登录...",
                mask: true,
                success: function () {
                    let postData = {
                        mobile_no: e.detail.value.user_phone,
                        verify_code: e.detail.value.user_code,
                        visitorId: wx.getStorageSync('visitorId'),
                        usertoken: wx.getStorageSync("usertoken")
                    }
                    wx.setStorageSync("userPhone", e.detail.value.user_phone);
                    let url = config.phoneLogin
                    $.post(url, postData).then(data => {

                        let postData = data
                        wx.hideLoading();
                        if (data.result != 1) {
                            $.showToast(data.message)
                        } else {
                            if (data.code == 1) {
                                wx.showModal({
                                    title: '恭喜您获得10元现金',
                                    content: '请至个人中心余额查看！ 快来开启千本好书借阅之旅~',
                                    showCancel: false,
                                    success: res => {
                                        if (res.confirm) {
                                            $.goto(config.page.index, true)
                                        }
                                    }
                                })
                            } else {
                                $.goto(config.page.index, true)
                            }
                            user.saveLoginInfo(data.detail)
                        }
                    })
                }

            });

        }
    },

    despoit_webview: function () {
        app.globalData.webview = {
            url: config.licenseUrl,
            title: '用户协议'
        };
        $.goto(config.page.webview);
    },


})